import 'package:flutter/material.dart';

class DemoPageMain extends StatefulWidget {
  static const ROUTER_NAME = "DemoPageMain";

  @override
  _DemoPageMainState createState() => _DemoPageMainState();
}

class _DemoPageMainState extends State<DemoPageMain> {
  int _bottomTabIndex = 0;
  PageController _pageViewController = new PageController(initialPage: 0);

//  todo 添加对应tab的页面  页面实现AutomaticKeepAliveClientMixin 缓存状态
  List<Widget> _pages = [
    SafeArea(
      child: Center(
        child: Text("page1"),
      ),
    ),
    SafeArea(
      child: Center(
        child: Text("page2"),
      ),
    ),
    SafeArea(
      child: Center(
        child: Text("page3"),
      ),
    ),
    SafeArea(
      child: Center(
        child: Text("page4"),
      ),
    ),
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: PageView(
        controller: _pageViewController,
//        physics: NeverScrollableScrollPhysics(),
        children: _pages,
        onPageChanged: (index) {
          if (_bottomTabIndex == index) {
            return;
          }
          setState(() {
            _bottomTabIndex = index;
          });
        },
      ),
      bottomNavigationBar:BottomAppBar(child:  BottomNavigationBar(
//        todo 设置底部按钮和样式
        items: [
          BottomNavigationBarItem(
            icon: Icon(Icons.home),
            title: Text("首页"),
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.message),
            title: Text("消息"),
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.shopping_cart),
            title: Text("购物车"),
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.person),
            title: Text("个人中心"),
          ),
        ],
        currentIndex: _bottomTabIndex,
        onTap: (index) {
//          _pageController.jumpToPage(index);
          _pageViewController.animateToPage(index,
              duration: Duration(milliseconds: 500), curve: Curves.easeInOut);
          setState(() {
            _bottomTabIndex = index;
          });
        },
        type: BottomNavigationBarType.fixed,
      ),shape: CircularNotchedRectangle(),),
//      floatingActionButton: FloatingActionButton(onPressed: (){
//        print("FloatingActionButton");
//      }),
//      floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
    );
  }
}
